<?php

namespace App\Module\Api\Controllers;

use App\Exceptions\Customize\ParamsException;
use App\Module\Api\Controllers\Request\UserUpdateSubmitRequest;
use App\Module\Api\Validator\UserUpdateSubmitValidator;
use App\System\Cis\Repositories\CisUserRepository;
use Illuminate\Http\Request;


class UserUpdateSubmitController extends Controller
{
    public function __construct()
    {
        $this->query=new UserUpdateSubmitRequest();
        parent::__construct();
    }

    public function index(){
        (new UserUpdateSubmitValidator())->check();
        $userInfo = $this->query->userInfo;
        $cache = \Cache::get(\request()->s);
        if (!isset($userInfo['id'])){
            throw new ParamsException([
                'error_code'=>'1000',
                'message'=>'参数错误'
            ]);
        }
//        判断传入参数数量是否正确
        if (!isset($cache['userInfo']->id)||count($userInfo) != 6||count($userInfo['region'])!=2){
            throw new ParamsException([
               'error_code'=>'1000',
               'message'=>'参数错误'
            ]);
        }
        $user_id = $cache['userInfo']->id;
        $userInfo = array_except($userInfo,['id']);
        $user = CisUserRepository::getInstance();
        $flag = $user->updateById($user_id,$userInfo);
        if (!$flag){
            throw new ParamsException([
                'error_code'=>'1000',
                'message'=>'更新失败'
            ]);
        }
        return apiResponse(['d'=>'更新成功']);
    }
}
